<template>
  <div class="agreementNext">
    <AgreemnetList
      :linklist="linklist"
      :tableData="tableData"
      :btnList="btnList"
      :titleList="titleList"
    />
    <Collect :collect="collect" :money="money" />
    <Paging :total="totalSize" @getPage="getPage" @getLimit="getLimit" />
  </div>
</template>

<script>
import Paging from "../../components/Paging.vue";
import AgreemnetList from "../../components/List.vue";
import Collect from "../../components/collect.vue";
import { agreementList } from "../../http/apis/agreementApi";
import { timeFormat } from "../../utils/utils";
import { easySearch } from '../../http/apis/agreementApi'
import { mapState } from "vuex";
export default {
  data() {
    return {
      linklist: [
        {
          id: 0,
          name: "客户名称",
          isRouter: true,
          url: "/information/contranct",
        },
        {
          id: 1,
          name: "合同编号",
          isRouter: true,
          url: "/agreement/contractNumber",
        },
        { id: 2, name: "订单编号", isRouter: false },
        { id: 3, name: "起始日期", isRouter: false },
        { id: 4, name: "到期日期", isRouter: false },
        { id: 5, name: "合同分类", isRouter: false },
        { id: 6, name: "总金额", isRouter: false },
        { id: 7, name: "已收款", isRouter: false },
        { id: 8, name: "欠款", isRouter: false },
        { id: 9, name: "提供发票", isRouter: false },
        { id: 10, name: "是否含税", isRouter: false },
        {
          id: 11,
          name: "合同状态",
          isRouter: true,
          url: "/agreement/contranctStatus?id=1",
        },
        { id: 12, name: "审核人员", isRouter: false },
        { id: 13, name: "审核时间", isRouter: false },
        { id: 14, name: "备注", isRouter: false },
        { id: 15, name: "业务员", isRouter: false },
        { id: 16, name: "录入时间", isRouter: false },
        { id: 17, name: "附件", isRouter: true, url: "/agreement/appendix" },
      ],
      tableData: [],
      btnList: [
        { id: 1, name: "续费", type: "合同" },
        { id: 2, name: "到款", type: "合同" },
        { id: 3, name: "修改", type: "合同" },
        { id: 4, name: "删除", type: "合同" },
      ],
      titleList: [
        "customer_Name", //客户名称
        "contract_no", //合同编号
        "order_id", //订单编号
        "contract_startday", //起始日期
        "contract_endday", //到期日期
        "contract_part", //合同分类
        "contract_summoney", //总金额
        "contract_receivemoney", //已收款
        "contract_debtmoney", //欠款
        "contract_tax", //提供发票
        "contract_invoice", //是否含税
        "contract_status_no", //合同状态
        "status_id", //审核人员
        "status_startday", //审核时间
        "contract_comment", //备注
        "employee_id", //业务员
        "contract_enterday", //录入时间
        "appendix", //附件
      ],
      collect: [
        // '汇总统计','总金额','已收款','欠款'
        { id: 1, name: "汇总统计" },
        { id: 2, name: "总金额" },
        { id: 3, name: "已收款" },
        { id: 4, name: "欠款" },
      ],
      money: [],
      limit: 3,
      page: 1,
      totalSize: 0,
    };
  },

  components: {
    Paging,
    AgreemnetList,
    Collect,
  },

  created(){
    this.sendRequest();
  },

  mounted() {
    this.Request();
  },

  computed:{
  ...mapState(['searchVal'])
  },

  methods: {
    computed() {
      this.money = [];
      var totalMoney = 0;
      var hadreture = 0;
      var debt = 0;
      for (var a = 0; a < this.tableData.length; a++) {
        totalMoney += this.tableData[a].contract_summoney;
        hadreture += this.tableData[a].contract_receivemoney;
        debt += this.tableData[a].contract_debtmoney;
      }
      this.money.push(totalMoney);
      this.money.push(hadreture);
      this.money.push(debt);
      // console.log(this.money);
    },

    getLimit(val) {
      this.limit = val;
      // console.log(this.limit);
      // this.money = [];
      this.Request();
      this.sendRequest();
    },

    getPage(val) {
      this.page = val;
      // console.log(this.page);
      // this.money = [];
      this.Request();
      this.sendRequest();
    },

    //请求List
    Request() {
      agreementList(this.page, this.limit)
        .then((res) => {
          console.log("请求成功", res.data.data);
          this.totalSize = res.data.count;
          this.tableData = [];
          for( var a = 0 ; a < res.data.data.length ; a++ ){

            if(res.data.data[a].status_id == 0 ){
              res.data.data[a].status_id = '超级管理员'
              res.data.data[a].status_startday = timeFormat(res.data.data[a].status_startday)
            }else if(res.data.data[a].status_id == 1 ){
              res.data.data[a].status_id = '其他'
              res.data.data[a].status_startday = timeFormat(res.data.data[a].status_startday)
            }else{
              res.data.data[a].status_id = ''
              res.data.data[a].status_startday = ''
            }

            //附件
            if(!res.data.data[a].appendix){
              res.data.data[a].appendix = '点击添加附件'
            }

            //改变合同状态
            if (res.data.data[a].contract_status_no == 1) {
              res.data.data[a].contract_status_no = "合同有效";
            } else if(res.data.data[a].contract_status_no == 2) {
              res.data.data[a].contract_status_no = "合同无效";
            }else if(res.data.data[a].contract_status_no == 3){
              res.data.data[a].contract_status_no = "转审核";
            }
            //转换时间
            res.data.data[a].contract_startday = timeFormat(
               res.data.data[a].contract_startday
            );
            res.data.data[a].contract_endday = timeFormat(
              res.data.data[a].contract_endday
            );
            res.data.data[a].contract_enterday = timeFormat(
              res.data.data[a].contract_enterday
            );

            if (res.data.data[a].contract_tax === "1") {
              res.data.data[a].contract_tax = "是";
            } else {
              res.data.data[a].contract_tax = "否";
            }
            if (res.data.data[a].contract_invoice === 1) {
              res.data.data[a].contract_invoice = "是";
            } else {
              res.data.data[a].contract_invoice = "否";
            }

            if(res.data.data[a].employee_id == 0 ){
              res.data.data[a].employee_id = '超级管理员'
            }else{
              res.data.data[a].employee_id = '其他'
            }

            if(res.data.data[a].contract_part == 1){
              res.data.data[a].contract_part = '买卖'
            }else if(res.data.data[a].contract_part == 2 ){
              res.data.data[a].contract_part = '租赁'
            }else if(res.data.data[a].contract_part == 3 ){
              res.data.data[a].contract_part = '工程'
            }else if(res.data.data[a].contract_part == 4 ){
              res.data.data[a].contract_part = '技术'
            }else if(res.data.data[a].contract_part == 5 ){
              res.data.data[a].contract_part = '委托'
            }else {
              res.data.data[a].contract_part = '其他'
            }

            // console.log(this.$route.query.data);
            this.tableData.push(res.data.data[a]);
          }
          // this.tableData = res.data.data;
          this.computed();
          console.log(this.tableData);
        })
        .catch((err) => {
          console.log("请求失败", err);
        });
    },

    //搜索请求
    sendRequest(val) {
      this.tableData = [];
      easySearch(val)
        .then((res) => {
          console.log('请求成功',res);
          if(res.data.code == 200){
            console.log("搜索处理成功", res);
            for( let a = 0; a < res.data.data.length;a++ ){
              this.tableData.push(res.data.data[a])
              this.computed()
            if (res.data.data[a].contract_tax === "1") {
              res.data.data[a].contract_tax = "是";
            } else {
              res.data.data[a].contract_tax = "否";
            }
            if (res.data.data[a].contract_invoice === 1) {
              res.data.data[a].contract_invoice = "是";
            } else {
              res.data.data[a].contract_invoice = "否";
            }

            //改变合同状态
            if (res.data.data[a].contract_status_no == 1) {
              res.data.data[a].contract_status_no = "合同有效";
            } else if(res.data.data[a].contract_status_no == 2) {
              res.data.data[a].contract_status_no = "合同无效";
            }else if(res.data.data[a].contract_status_no == 3){
              res.data.data[a].contract_status_no = "转审核";
            }

            //合同分类
            if(res.data.data[a].contract_part == 1){
              res.data.data[a].contract_part = '买卖'
            }else if(res.data.data[a].contract_part == 2 ){
              res.data.data[a].contract_part = '租赁'
            }else if(res.data.data[a].contract_part == 3 ){
              res.data.data[a].contract_part = '工程'
            }else if(res.data.data[a].contract_part == 4 ){
              res.data.data[a].contract_part = '技术'
            }else if(res.data.data[a].contract_part == 5 ){
              res.data.data[a].contract_part = '委托'
            }else {
              res.data.data[a].contract_part = '其他'
            }

            //业务员
            if(res.data.data[a].employee_id == 0 ){
              res.data.data[a].employee_id = '超级管理员'
            }else{
              res.data.data[a].employee_id = '其他'
            }
            
          }
          }else{
            console.log("搜索处理失败", res);
          }
        })
        .catch((err) => {
          console.log("搜索订单列表处理错误", err);
        });
    },
  },
  
  watch: {
    searchVal(val) {
      if (!val) return;
      this.sendRequest(val);
    },
  },
};
</script>

<style>
</style>